<?php
require_once(realpath(dirname(__FILE__)).'/../../core/ClassLoader.class.php');


import('miho.controller.FrontController');
import('miho.config.*');
import('miho.logger.*');
import('miho.view.*');
import('miho.default.controller.*');


/**
 * Project context
 *
 * @package default
 * @author miho
 */
class ProjectConfigurator
{
  protected $_resources = array();
  
  public function registerResource(IProjectConfiguratorResource $resource){
  	$this->_resources[] = $resource;
  }
	
	protected $_envoirment='prod';
  public function getEnvoirment(){
    return $this->_envoirment;
  }
  
  protected $_rootDir;
  public function getRootDir(){
    return $this->_rootDir;
  }
  
  protected $_appsPathName = 'app';
  public function getAppPathName(){
    return $this->_appsPathName;
  }
  
  protected $_configDirName = 'config';
  
  protected $_request;
  protected $_requestClass = 'miho.request.http.HttpRequest';
  
  protected $_response;
  protected $_responseClass = 'miho.response.http.HttpResponse';
  
  protected $_router;
  protected $_routerClass  = 'miho.router.pattern.PatternRouter';
  public function getRouter(){
    return $this->_router;
  }
  
  protected $_defaultAppPath = 'miho.default';
  protected $_defaultAppliction = 'default';
  protected $_defaultConrtoller = 'default';
  protected $_defaultAction = 'index';
  
  protected $_application = 'default';
  protected $_controller = 'default';
  protected $_action = 'index';
  
  
  protected $_errorHandlerClass = 'miho.debug.handler.ErrorHandler';
  protected $_errorHandler;
  
  public function __construct($env){
    $this->_envoirment= $env;
    import("miho.config.configurator.resource.FrontControllerResource");
    $this->registerResource(new FrontControllerResource());
    $this->setup();
    ClassLoader::getInstance()->addClassPath(realpath($this->_rootDir.'/'.$this->_appsPathName));
  }
  
  public function setup(){
    
  }
  
  public function initConfig(){
  	var_dump($this->_resources);
    foreach ($this->_resources as $resource) {
    	
    	$config = $resource->getConfigName();
    	
    	$file = $this->_rootDir.'/config/'.$config.'.json';
    	
    	if(file_exists($file))
    	{
    		$options = Config::getInstance()->getFromFile($file);
    		
    		var_dump($options->getValues());
    		
    		$resource->init($options->getValues());
  		}
    }
  	exit();
  	$this->_initHandlers();
    $this->_initDb();
    $this->_initController();
    $this->_initView();
        
  }
  
  protected function _initDb(){
    import('miho.db.Connection');
    Config::getInstance()->loadFromFile($this->_rootDir.'/config/db.json','db_');
    
    $dsn = Config::get('db_'.$this->_envoirment.'_dsn');
    $connection = Connection::makeConnection($dsn);
  }
  
  
  protected function _initController(){
    Config::getInstance()->loadFromFile($this->_rootDir.'/config/route.json');
    
    import ($this->_requestClass);
    $request = ClassLoader::getInstance()->guessClassName($this->_requestClass);
    $this->_request = new $request();
    
    import($this->_routerClass);
    $router = ClassLoader::getInstance()->guessClassName($this->_routerClass);
    $this->_router = new $router($this->_request->getQuery());
    
    $routerConfig = Config::getInstance()->getFromFile($this->_rootDir.'/config/route.json');
    $this->_router->configure($routerConfig->getValues());
    
  }
  
  protected function _initView(){
    $view = View::getInstance();
    $view->setTemplatesHome($this->_rootDir.'/app');
    $view->initialize();
    /*import($this->_responseClass);
    $response = ClassLoader::getInstance()->guessClassName($this->_responseClass);
    $this->_response = new $response();*/
  }
  
  protected function _initHandlers(){
    import($this->_errorHandlerClass);
    $errorHandler = ClassLoader::getInstance()->guessClassName($this->_errorHandlerClass);
    $this->_errorHandler = new $errorHandler();
    set_error_handler(array($this->_errorHandler,'handleError'), E_ERROR );
    set_exception_handler(array($this->_errorHandler,'handleException'));
  }
  
  public function run(){
  	foreach ($this->_resources as $resource) {
  		$resource->run();
  	}
  }

}


?>  